﻿using System;
using System.Windows.Input;
using AesonXamlControls.Test.Config;
using AesonXamlControls.Test.Helpers.Navigation;
using AesonXamlControls.Test.LocalConsole;
using Microsoft.Expression.Interactivity.Core;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Logging;
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Prism.Mvvm;
using Microsoft.Practices.Prism.Regions;

namespace AesonXamlControls.Test.Views.MainViews
{
    public class MainWindowsViewModel : BindableBase
    {
        #region Private Member from Ctor
        private IModuleManager ModuleManager { get; set; }
        private IRegionManager RegionManager { get; set; }
        #endregion

        #region AesonConsole

        private AesonClientConsole ConsoleWindow { get; set; }
        public ICommand ShowConsoleCommand { get; private set; }

        public DelegateCommand MyCommand { get; set; }
        private void ShowConsole()
        {
            ConsoleWindow.ShowConsole();
        }

        #endregion

        #region Menus
        #region MainMenu
        private bool _isMainOpen;
        public bool IsMainMenuOpen
        {
            get { return _isMainOpen; }
            set { SetProperty(ref this._isMainOpen, value); }
        }

        public ICommand MainMenuCommand { get; private set; }
        public void HandleMainMenu()
        {
            IsMainMenuOpen = !IsMainMenuOpen;
        }

        public ICommand GoToAesonTextBoxCommand { get; private set; }

        private void GoToAesonTextBox()
        {
            NavTo(ViewNames.AesonTextBox);
        }

        public ICommand GoToAesonDataFormCommand { get; private set; }

        private void GoToAesonDataForm()
        {
            NavTo(ViewNames.DataFormView);
        }


        #region AccountingMenu
        public ICommand AccountingMenuCommand { get; set; }
        private bool _isAccountingbarOpen;
        public bool IsAccountingbarOpen
        {
            get { return _isAccountingbarOpen; }
            set { SetProperty(ref this._isAccountingbarOpen, value); }
        }

        private void ShowAccountingMenu()
        {
            IsAccountingbarOpen = !IsAccountingbarOpen;
        }

        #endregion

        #region AdminMenu
        public ICommand AdminMenuCommand { get; set; }

        private bool _isAdminbarOpen;
        public bool IsAdminbarOpen
        {
            get { return _isAdminbarOpen; }
            set { SetProperty(ref this._isAdminbarOpen, value); }
        }

        private void ShowAdminMenu()
        {
            IsAdminbarOpen = !IsAdminbarOpen;
        }
        #endregion

        #endregion

        #region HomeCommand
        public ICommand HomeCommand { get; private set; }
        private void NavigateHome()
        {
            this.RegionManager.RequestNavigate(RegionNames.MainRegion, ViewNames.HomePage);
        }
        #endregion

        #endregion



        public MainWindowsViewModel(IModuleManager moduleManager, IRegionManager regionManager, AesonClientConsole console, ILoggerFacade logger)
        {
            logger.Log("MainWindowsViewModel loaded", Category.Debug, Priority.Low);
            #region Assign Field
            this.ModuleManager = moduleManager;
            this.RegionManager = regionManager;
            this.ConsoleWindow = console;
            #endregion

            #region Assign Command
            this.MainMenuCommand = new DelegateCommand(HandleMainMenu);
            this.HomeCommand = new DelegateCommand(NavigateHome);
            this.ShowConsoleCommand = new DelegateCommand(ShowConsole);
            this.MyCommand = new DelegateCommand(ShowConsole);
            this.AccountingMenuCommand = new DelegateCommand(ShowAccountingMenu);
            this.AdminMenuCommand = new DelegateCommand(ShowAdminMenu);
            this.GoToAesonTextBoxCommand = new DelegateCommand(GoToAesonTextBox);
            this.GoToAesonDataFormCommand = new DelegateCommand(GoToAesonDataForm);
            #endregion;


            this.ModuleManager.LoadModuleCompleted += ModuleManager_LoadModuleCompleted;
        }

        private void ModuleManager_LoadModuleCompleted(object sender, LoadModuleCompletedEventArgs e)
        {
            if (e.ModuleInfo.ModuleName == ModuleNames.HomeModuleName)
            {
                this.RegionManager.MainRegionNavigate(ViewNames.HomePage);
            }

        }

        private void NavTo(string s)
        {

            this.RegionManager.MainRegionNavigate(s);
            IsMainMenuOpen = false;
        }


    }
}
